package com.app.server.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.app.common.SystemConstant;
import com.app.mapper.AreaMapper;
import com.app.mapper.UserMapper;
import com.app.model.AreaModel;
import com.app.model.UserModel;
import com.app.server.UserService;
import com.app.utils.CommonHelp;
import com.app.utils.WechatUtil;
import com.app.vo.UserVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserModel> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AreaMapper areaMapper;

    @Override
    public void update(UserModel model) {
        userMapper.updateById(model);
    }

    @Override
    public void insert(UserModel model) {
        userMapper.insert(model);
    }

    @Override
    public UserModel selectById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public UserVo getList(Integer page, Integer limit, String keyword) {
        QueryWrapper<UserModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.ne("phone", "").or().isNotNull("phone");
        queryWrapper.like("name", keyword).or().like("phone", keyword);
        queryWrapper.orderByDesc("id");
        Page<UserModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<UserModel> userModelPage = userMapper.selectPage(pages, queryWrapper);
        List<UserModel> userList = userModelPage.getRecords();
        for (UserModel userModel : userList) {
            userModel.setOpenId(null);
        }
        UserVo vo = new UserVo();
        vo.setList(userModelPage.getRecords());
        vo.setCount((int) userModelPage.getTotal());
        return vo;
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        UserModel model = new UserModel();
        model.setId(id);
        model.setStatus(status);
        userMapper.updateById(model);
    }

    @Override
    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public UserModel getOpenid(String code, HttpServletRequest request) throws LoginException {
        //获取openid
        String openid = WechatUtil.getOpenid(code);
        if (openid == null) {
            throw new LoginException("openid为空");
        }
        //根据openid查询用户信息
        UserModel user = userMapper.getByOpenid(openid);
        if (user == null) {
            //当前是新用户，自动注册
            user = new UserModel();
            user.setAvatar(SystemConstant.DEFAULT_FACE_URL);//默认头像
            user.setName("用户" + RandomUtil.randomString(8));//默认昵称
            user.setPhone("");
            user.setStatus(1);
            user.setOpenId(openid);
            user.setCreateTime(CommonHelp.getNowTimestamp());
            userMapper.insert(user);
        }
        return user;
    }

    @Override
    public UserModel getPhone(String code, HttpServletRequest request, UserModel user) {
        String mobile = WechatUtil.getMobile(code);
        user.setPhone(mobile);//存手机号
        user.setUpdateTime(CommonHelp.getNowTimestamp());
        userMapper.updateById(user);
        return user;
    }

    @Override
    public UserModel getUserByOpenid(String mobile) {
        QueryWrapper<UserModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("open_id", mobile);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public UserModel selectUserById(Integer id) {
        QueryWrapper<UserModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "name", "open_id", "phone", "avatar", "status", "area_id","create_time", "update_time");
        queryWrapper.eq("id", id);
        UserModel userModel = userMapper.selectOne(queryWrapper);

        if (StrUtil.isNotBlank(userModel.getAreaId())) {
            List<AreaModel> area = new ArrayList<>();
            for (String string : userModel.getAreaId().split(",")) {
                AreaModel areaModel = areaMapper.selectById(string);
                areaModel.setValue(areaModel.getId());
                areaModel.setText(areaModel.getName());
                area.add(areaModel);
            }
            userModel.setArea(area);
        }
        return userModel;
    }
}
